//
//  LzwUserViewController.m
//  NewHalfsugar
//
//  Created by 刘志武 on 15/12/31.
//  Copyright © 2015年 刘志武. All rights reserved.
//

#import "LzwUserViewController.h"
#import "LzwBaseView.h"
#import "LzwLandingViewController.h"
#import "LzwBaseImageView.h"
#import "LzwBaseLable.h"
#import "LzwBaseButton.h"
#import "LzwAttentionVC.h"
#import "LzwIdearVC.h"
#import "SDImageCache.h"
#import "LzwChoutiVC.h"
#import "LzwbaseTableView.h"
#import "LzwBaseTableViewCell.h"
#import "UMSocial.h"
#import "LzwUserSettingVC.h"
#import "UIImageView+WebCache.h"
#import "LzwUserPageWebViewVc.h"

@interface LzwUserViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, retain) LzwbaseTableView *myUserTableView;
@property (nonatomic, retain) UISwitch *switchView;
@property (nonatomic, retain) UILabel *myLable;
@property (nonatomic, retain) LzwBaseImageView *yongHuImage;

@property (nonatomic, retain) UMSocialAccountEntity *snsAccount;

@end

@implementation LzwUserViewController

-(void)dealloc{
    [_myLable release];
    [_myUserTableView release];
    [super dealloc];
    
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
    
    NSString *str = [[NSUserDefaults standardUserDefaults] objectForKey:@"night"];
    if ([str isEqualToString:@"yes"]) {
        self.switchView.on = YES;
    }else{
        self.switchView.on = NO;
    }
    
    
    
    [self showButtonStates];
}

- (void)showButtonStates{
    
    NSString *states = [[NSUserDefaults standardUserDefaults]objectForKey:@"states"];
    
    if ([states isEqualToString:@"YES"]) {
     
        self.navigationItem.rightBarButtonItem.title = @"注销";
        
    }else{
        self.navigationItem.rightBarButtonItem.title = @"登陆";

        
    }
    
}



- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
//    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationController.navigationBarHidden = NO;
    
    self.navigationItem.title = @"我的";
    [self.navigationController.navigationBar setTitleTextAttributes:
     
     
     @{NSFontAttributeName:[UIFont systemFontOfSize:20 * WIDTH],
       
       
       NSForegroundColorAttributeName:[UIColor whiteColor]}];
    
    
    
 
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"登陆" style:UIBarButtonItemStylePlain target:self action:@selector(RightButtonAction:)];
    
    self.navigationItem.rightBarButtonItem.tintColor = [UIColor whiteColor];
    [self.navigationItem.rightBarButtonItem setTitleTextAttributes: @{NSFontAttributeName:[UIFont systemFontOfSize:20 * WIDTH]} forState:UIControlStateNormal];
    
    
    UIImage *leftBar = [UIImage imageNamed:@"setting2.png"];
    leftBar = [leftBar imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithImage:leftBar style:UIBarButtonItemStylePlain target:self action:@selector(ReturnButtonAction:)];
    
    
    self.myUserTableView = [[LzwbaseTableView alloc]initWithFrame:self.view.bounds];
    [self.view addSubview:self.myUserTableView];
    
    self.myUserTableView.delegate = self;
    self.myUserTableView.dataSource = self;
    
    
    
    self.myUserTableView.tableHeaderView = [self creatHead];
    self.myUserTableView.tableFooterView = [self creatFoot];
    

    
}

- (LzwBaseView *)creatHead{
    
    LzwBaseView *myView = [[LzwBaseView alloc]initWithFrame:CGRectMake(0, 0,  375 * WIDTH, 280 * WIDTH)];
//    myView.backgroundColor = [UIColor yellowColor];
    

    
    LzwBaseImageView *userImage = [[LzwBaseImageView alloc]initWithFrame:myView.bounds];
    userImage.image = [UIImage imageNamed:@"User.png"];
    userImage.contentMode = UIViewContentModeScaleAspectFill;
    [myView addSubview:userImage];

    //给用户图片添加轻拍手势
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(aaaaaa:)];
    
    self.yongHuImage = [[LzwBaseImageView alloc]initWithFrame:CGRectMake((375 - 80) / 2 * WIDTH, (280 - 80) / 2 - 40 * WIDTH, 80 * WIDTH, 80 * WIDTH)];
    self.yongHuImage.image = [UIImage imageNamed:@"one.png"];
    self.yongHuImage.layer.masksToBounds = YES;
    self.yongHuImage.userInteractionEnabled = YES;
    [self.yongHuImage addGestureRecognizer:tap];
    self.yongHuImage.layer.cornerRadius = 40 * WIDTH;
    
    [myView addSubview:self.yongHuImage];
    
    LzwBaseImageView *right = [[LzwBaseImageView alloc]initWithFrame:CGRectMake(90 * WIDTH, 90 * WIDTH, 30 * WIDTH, 30 * WIDTH)];
    right.image = [UIImage imageNamed:@"WhiteXin.png"];
    [userImage addSubview:right];
    
    LzwBaseImageView *left = [[LzwBaseImageView alloc]initWithFrame:CGRectMake(260 * WIDTH, 90 * WIDTH, 25 * WIDTH, 25 * WIDTH)];
    left.image = [UIImage imageNamed:@"20.png"];
    [userImage addSubview:left];
    
    self.myLable = [[UILabel alloc]initWithFrame:CGRectMake((375 - 100) / 2 * WIDTH, 160 * WIDTH, 100 * WIDTH, 20 * WIDTH)];
    self.myLable.font = [UIFont systemFontOfSize:15 * WIDTH];
    self.myLable.textAlignment = NSTextAlignmentCenter;
//    lable1.text = @"我想要的生活";
    [userImage addSubview:self.myLable];
    
 
    [_yongHuImage release];
    [right release];
    [left release];
    [_myLable release];
    
    return myView;
}



- (LzwBaseView *)creatFoot{
    
    LzwBaseView *viewFoot = [[LzwBaseView alloc]initWithFrame:CGRectMake(0, 0, 375 * WIDTH, 70 * WIDTH)];
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(50 * WIDTH, 10 * WIDTH, 272 * WIDTH, 50 * WIDTH);
    button.backgroundColor = [UIColor orangeColor];
    button.layer.masksToBounds = YES;
    button.layer.cornerRadius = 25 * WIDTH;
    [button setTitle:@"使用其他方式登陆" forState:UIControlStateNormal];
    [button setTitle:@"请稍等......." forState:UIControlStateHighlighted];
    [button setTitleColor:[UIColor greenColor] forState:UIControlStateHighlighted];
    [viewFoot addSubview:button];
    
    [button addTarget:self action:@selector(ButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    
    return viewFoot;
}


//第三方登陆分享
- (void)ButtonAction:(LzwBaseButton *)but{
    UMSocialSnsPlatform *snsPlatform = [UMSocialSnsPlatformManager getSocialPlatformWithName:UMShareToSina];

    
    snsPlatform.loginClickHandler(self,[UMSocialControllerService defaultControllerService],YES,^(UMSocialResponseEntity *response){
        
        //          获取微博用户名、uid、token等
        
        if (response.responseCode == UMSResponseCodeSuccess) {
            
            self.snsAccount = [[UMSocialAccountManager socialAccountDictionary] valueForKey:UMShareToSina];
            self.myLable.text = self.snsAccount.userName;
            
            NSURL *urlq = [NSURL URLWithString:self.snsAccount.iconURL];
            [self.yongHuImage sd_setImageWithURL:urlq placeholderImage:nil];
            NSLog(@"username is %@, uid is %@, token is %@ url is %@, profileURL is %@",_snsAccount.userName,_snsAccount.usid,_snsAccount.accessToken,_snsAccount.iconURL, _snsAccount
                  .profileURL);
            
    
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"登陆成功" preferredStyle:UIAlertControllerStyleAlert];
        [self presentViewController:alert animated:YES completion:^{
         
         
            [self dismissViewControllerAnimated:YES completion:^{
             
            }];
        }];
    
            [[NSUserDefaults standardUserDefaults] setObject:@"ch" forKey:@"Weibo"];
        }});
    
}
//给用户添加添加轻拍手势
- (void)aaaaaa:(UITapGestureRecognizer *)tap{
    
//    NSString *states = [[NSUserDefaults standardUserDefaults]objectForKey:@"Weibo"];
    
//    if ([states isEqualToString:@"no"]) {
    
    LzwUserPageWebViewVc *webVc = [[LzwUserPageWebViewVc alloc]init];
        webVc.string = self.snsAccount.profileURL;
        
    
        [self.navigationController pushViewController:webVc animated:YES];
//        return;
//    }
//    if ([states isEqualToString:@"ch"])
    
//    {
    
//        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"请您先进行登陆" preferredStyle:UIAlertControllerStyleAlert];
//        UIAlertAction *alerta = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
//            
//            
//        }];
//        
//        [self presentViewController:alert animated:YES completion:^{
//            
//            
//        }];
//        [alert addAction:alerta];
    
//    }
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 4;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString *cellindentifier = @"cell";
    
    LzwBaseTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellindentifier];
    
    if (!cell) {
        cell = [[LzwBaseTableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellindentifier];
    }
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    if (indexPath.row == 0) {
        UIImage *icon = [UIImage imageNamed:@"55.png"];
                LzwBaseImageView *myimage = [[LzwBaseImageView alloc]initWithImage:icon];
                myimage.frame = CGRectMake(18 * WIDTH, 18 * WIDTH, 15 * WIDTH, 15 * WIDTH);
                [cell addSubview:myimage];
        
        cell.detailTextLabel.text = @">";

                cell.indentationWidth = 20.0f * WIDTH;
                cell.indentationLevel = 2 * WIDTH;
                cell.textLabel.text = @"我的关注";
        cell.textLabel.textColor = [UIColor orangeColor];
    }else if (indexPath.row == 1){
       
        UIImage *icon = [UIImage imageNamed:@"sun.png"];
        LzwBaseImageView *myimage = [[LzwBaseImageView alloc]initWithImage:icon];
        myimage.frame = CGRectMake(15 * WIDTH, 15 * WIDTH, 20 * WIDTH, 20 * WIDTH);
        [cell.contentView addSubview:myimage];
        
        UISwitch *mySwitch = [[UISwitch alloc]initWithFrame:CGRectMake(300 * WIDTH, 10 * WIDTH, 40 * WIDTH, 20 * WIDTH)];
        [cell addSubview:mySwitch];
        
        [mySwitch addTarget:self action:@selector(MySwitchAction:) forControlEvents:UIControlEventTouchUpInside];
        //判断按钮状态
        
        [[NSUserDefaults standardUserDefaults] objectForKey:@"night"];

        
        if ([[[NSUserDefaults standardUserDefaults] objectForKey:@"night"] isEqualToString:@"yes"]) {
            [mySwitch setOn:YES];
        } else if([[[NSUserDefaults standardUserDefaults] objectForKey:@"night"] isEqualToString:@"no"]){
            [mySwitch setOn:NO];
        }
        
        cell.indentationWidth = 20.0f * WIDTH;
        cell.indentationLevel = 2 * WIDTH;
        cell.textLabel.text = @"夜间模式";
        cell.textLabel.textColor = [UIColor orangeColor];
        

        
    }else if (indexPath.row == 2){
        UIImage *icon = [UIImage imageNamed:@"bianji.png"];
        LzwBaseImageView *myimage = [[LzwBaseImageView alloc]initWithImage:icon];
        myimage.frame = CGRectMake(15 * WIDTH, 15 * WIDTH, 20 * WIDTH, 20 * WIDTH);
        [cell addSubview:myimage];
        cell.detailTextLabel.text = @">";

        cell.indentationWidth = 20.0f * WIDTH;
        cell.indentationLevel = 2 * WIDTH;
        cell.textLabel.text = @"意见反馈";
        cell.textLabel.textColor = [UIColor orangeColor];

    }else if (indexPath.row == 3){
        UIImage *icon = [UIImage imageNamed:@"shanchu.png"];
        LzwBaseImageView *myimage = [[LzwBaseImageView alloc]initWithImage:icon];
        myimage.frame = CGRectMake(15 * WIDTH, 15 * WIDTH, 23 * WIDTH, 23 * WIDTH);
        [cell.contentView addSubview:myimage];
        
        cell.detailTextLabel.text = @">";

        cell.indentationWidth = 20.0f * WIDTH;
        cell.indentationLevel = 2 * WIDTH;
        cell.textLabel.text = @"清除缓存";
        cell.textLabel.textColor = [UIColor orangeColor];

        
    }
    
    
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 50;
    
}

//点击登陆按钮触发事件
- (void)RightButtonAction:(UIBarButtonItem *)bar{
    
    LzwLandingViewController *landVC = [[LzwLandingViewController alloc]init];
    
    
    NSString *string = [[NSUserDefaults standardUserDefaults]objectForKey:@"states"];
    
    if ([string isEqualToString:@"YES"]) {
        
        self.navigationItem.rightBarButtonItem.title = @"登陆";
        
        [[NSUserDefaults standardUserDefaults] setObject:@"NO" forKey:@"states"];
        return;
    }else{
    
        self.navigationItem.rightBarButtonItem.title = @"注销";
    
    UINavigationController *naVC = [[UINavigationController alloc]initWithRootViewController:landVC];
    
        [self presentViewController:naVC animated:YES completion:^{
            
            
        }];
    
    }
    [[NSUserDefaults standardUserDefaults] synchronize];

}

//点击设置按钮触发事件
- (void)ReturnButtonAction:(UIBarButtonItem *)bar{
    
    LzwUserSettingVC *userSettingVc = [[LzwUserSettingVC alloc]init];
    [self.navigationController pushViewController:userSettingVc animated:YES];
    
    
}
//点击cell触发事件
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.row == 0) {
        LzwAttentionVC *atten = [[LzwAttentionVC alloc]init];
        [self.navigationController pushViewController:atten animated:YES];
        
    }
    if (indexPath.row == 1){
        
    }
    if (indexPath.row == 2){
        LzwIdearVC *ideaVC = [[LzwIdearVC alloc]init];
        [self.navigationController pushViewController:ideaVC animated:YES];
    }
    if (indexPath.row == 3) {
        

        float sizeMB = [[SDImageCache sharedImageCache]getSize] / 1024 / 1024;
        
        NSString *messageStr=[NSString stringWithFormat:@"缓存为%.1fMB是否清除缓存",sizeMB];
        
        
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:messageStr preferredStyle:UIAlertControllerStyleAlert];
        
      
        
        [self presentViewController:alert animated:YES completion:^{
         
            
            
         }];
        
        UIAlertAction *alert2 = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
            
            //点击确定就清除图片缓存数据
            [[SDImageCache sharedImageCache]clearDisk];
            
        }];
        
        UIAlertAction *alert3 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            
            
        }];
        
     
        
        [alert addAction:alert2];
        [alert addAction:alert3];
      
       
    }
    
    
}

//夜间模式
- (void)MySwitchAction:(UISwitch *)swi{

    NSString *states = [[NSUserDefaults standardUserDefaults] objectForKey:@"night"];
    if ([states isEqualToString:@"yes"]) {
        //夜间模式
        [[NSUserDefaults standardUserDefaults] setObject:@"no" forKey:@"night"];
        //然后发送通知
        [[NSNotificationCenter defaultCenter] postNotificationName:@"changeColor" object:[UIColor whiteColor]];
    }else{
        
        [[NSUserDefaults standardUserDefaults] setObject:@"yes" forKey:@"night"];
        //发送通知
        [[NSNotificationCenter defaultCenter] postNotificationName:@"changeColor" object:[UIColor blackColor]];
        
    }
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
